(function(KKBus){

    function BusStopController() {
        var self = this;
        
                
        self.coursesKrk = ko.observableArray();
        self.coursesKat = ko.observableArray();
        
        var sortFn = function (left, right) {
        return left.order() > right.order() ? 1 : -1;
            };
            
                    
        
            
                
        self.getCourses = function() {
            app.request++;
            
            
            KKBus.Ajax.doGet('rest/busstops', function(data, textStatus, jqXHR) {
                var jsData = JSON.parse(data);
                var i = jsData.length;
                
                while(i--) {                    
                    
                    var currentArrayCourses = null;
                    if (jsData[i].direction == "KRAKOW_KATOWICE") {
                        currentArrayCourses = self.coursesKrk;
                        
                    } else {
                        currentArrayCourses = self.coursesKat;
                    }   
                    
                    
                    var currentBusStop = new KKBus.BusStopModel(jsData[i].id, jsData[i].name, jsData[i].distance, jsData[i].order);
                    
                    currentArrayCourses.push(currentBusStop);
                    currentArrayCourses.sort(sortFn);

                    var j = jsData[i].departureTimes.length;
                    while(j--) {
                        var ref = jsData[i].departureTimes[j];
                        currentBusStop.departureTimes.push(new KKBus.DepartureTime(ref.id, ref.attributes, ref.time, ref.order)); 
                        currentBusStop.departureTimes.sort(sortFn);
                    }
                    
                    
                }
                
                app.request--;
                
                if (app.request == 0) {
                    if (app.settings.DEBUG == true) { console.log("Load template from: " + BusStopController.name); }
                    setTimeout(function() { app.loadTemplate(); }, 1000);
                }
            });
            
                 self.coursesKrk.removeAll();
                 self.coursesKat.removeAll();
            
        };

        
        self.viewCourses0 = ko.observable(false);
        self.viewCourses1 = ko.observable(false);
        
        self.showCourses = function() {
            
            if($('#formKierunek').val() == 0){
                self.viewCourses0(true);
                self.viewCourses1(false);
                //setTimeout(function() { self.viewCourses0(false); }, 5000);             
                
                
            } else {
               
                self.viewCourses1(true);
                self.viewCourses0(false);
                              
             
            }
            
            
        };

    }

    KKBus.BusStopController = BusStopController;
    
}(window.KKBus));

